#!/usr/bin/python

import WykopRespProfile
import SimpleRequest
import WykopRespLink
import WykopRespGroup
import WykopRespEntry

class WykopSearch(SimpleRequest.SimpleRequest):
    
    H_WykopAPI = None
    
    def _sng_query(self, types, Q, Page =None, What = None,Sort = None, Where = None, From = None, To = None, Votes = None):
        
        self._addElement('search')
        self._addElement(types)
        self._addPostElement('q',Q)
        if Page != None:
            self._addElement('page',str(Page))
        if What !=None:
            self._addPostElement('what',What)
        if Sort != None:
            self._addPostElement('sort',Sort)
        if Where != None:
            self._addPostElement('where',where)
        if From != None:
            self._addPostElement('from',From)
        if To != None:
            self._addPostElement('to',To)
        if Votes != None:
            self._addPostElement('votes',int(Votes))
            
        self._addAPIElement(self.H_WykopAPI._appkey, self.H_WykopAPI._userkey)
        self._send()
        return self._getData()
        
    def __init__(self, WykopAPIHandler):
        
        self.H_WykopAPI =  WykopAPIHandler
    
    def getIndex(self, Q):
        
        C = WykopRespLink.WykopRespLink()
        return C._normalizeData(self._sng_query('index',Q))
    
    def getLinks(self, types, Q, Page =None, What = None,Sort = None, Where = None, From = None, To = None, Votes = None):
        
        C = WykopRespLink.WykopRespLink()
        return C._normalizeData(self._sng_query('links',Q,Page,What,Sort,Where,From,To,Votes))
    def getEntries(self, Q, Page = None):
        
        C = WykopRespEntry.WykopRespEntry()
        return C._normalizeDatatoList(self._sng_query('entries',Q,Page))
    
    